﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;

namespace MyCommon
{
    public enum MyReportType
    {
        Report_PhieuTiepNhan = 1,
        Report_BaoHanhSuaChuaNgay,
        Report_TiepNhanBaoHanhSua,
        Report_PhienKhongDungHan,
    }

    public enum VaiTro
    {
        Admin = 1,
        NVTiepNhan,
        NVKyThuat,
        NVQuanLy
    }

    public enum TrangThaiPhien
    {
        ThaoTacNgay = 1,
        ThaoTacNgay_HoanThanh,
        TiepNhan_DoiBaoHanh,
        DoiBaoHanh_HoanThanh
    }
    public enum TrangThaiThietBiBH
    {
        BaoHanhSuaChuaNgay = 1,
        DoiBHSC,
        DangOPhongKT,
        DangONCC,
        DaNhanVeChuaHoanThanh,
        ChoTra,
        DaTra
    }
    public enum LoaiLichSu
    {
        LichSuHeThong = 1,  //Lich su chuyen trang thai, giao viec ..
        LichSuSuaChua,      //Lich su sua chua, ghi nhan chi phi
        LichSuGoiDien,      //Lich su cac lan goi dien cho khach hang
        LichSuComment          // Lich su ghi nhan ghi chu cua nhan vien, cua sep
    }
    public enum CodeType
    {
        KhachHang,
        NhanVien,
        NhaSanXuat,
        NhaCungCap,
        LoaiDichVu,
        Loi,
        ThietBiKho,
        ThietBiBaoHanh,
        Phien,
    }
    public static class Utils
    {   
        public static string GenerateCode( CodeType type, int index)
        {
            //TODO: Get latest id and gen
            string code = "";
            switch (type)
            {
                case CodeType.KhachHang:
                    //Generate ma khach hang
                    code = "KH";
                    code = GenFormat(code, index);
                    break;
                case CodeType.NhanVien:
                    //Generate ma nhan vien
                    code = "NV";
                    code = GenFormat(code, index, 3);
                    break;
                
                case CodeType.NhaSanXuat:
                    //Generate ma nha san xua
                    code = "NSX";
                    code = GenFormat(code, index, 3);
                    break;
                case CodeType.NhaCungCap:
                //Generate ma nha cung cap
                    code = "NCC";
                    code = GenFormat(code, index, 4);
                    break;
                case CodeType.LoaiDichVu:
                    //Generate ma loai dich vu
                    code = "DV";
                    code = GenFormat(code, index, 2);
                    break;
                case CodeType.Loi:
                    //Generate ma loi
                    code = "ML";
                    code = GenFormat(code, index, 3);
                    break;
                case CodeType.ThietBiKho:
                    //Generate ma thiet bi kho
                    code = "TBK";
                    code = GenFormat(code, index);
                    break;
                case CodeType.ThietBiBaoHanh:
                    //Generate ma thiet bi kho
                    code = "TBBH";
                    code = GenFormat(code, index);
                    break;
                case CodeType.Phien:
                    //Generate ma thiet bi kho
                    code = "TN";
                    code = GenFormat(code, index, 6);
                    break;
                default:
                    break;
            }
            return code;
        }
        private static string GenFormat(string prefix, int index, int number = 6)
        {
            //format AA0000001
            string retCode = prefix += "-";
            int lenIndex = index.ToString().Length;
            for (int i = 0; i <(number-lenIndex); i++)
            {
                retCode += "0";
            }
            retCode += index.ToString();
            return retCode;
        }

        /// <summary>
        /// Convert a List{T} to a DataTable.
        /// </summary>
        public static DataTable ToDataTable<T>(this IList<T> items)
        {
            var tb = new DataTable(typeof(T).Name);

            PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo prop in props)
            {
                Type t = GetCoreType(prop.PropertyType);
                tb.Columns.Add(prop.Name, t);
            }

            foreach (T item in items)
            {
                var values = new object[props.Length];

                for (int i = 0; i < props.Length; i++)
                {
                    values[i] = props[i].GetValue(item, null);
                }

                tb.Rows.Add(values);
            }

            return tb;
        }

        /// <summary>
        /// Determine of specified type is nullable
        /// </summary>
        public static bool IsNullable(Type type)
        {
            return !type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>));
        }

        /// <summary>
        /// Return underlying type if type is Nullable otherwise return the type
        /// </summary>
        public static Type GetCoreType(Type type)
        {
            if (type != null && IsNullable(type))
            {
                if (!type.IsValueType)
                {
                    return type;
                }
                else
                {
                    return Nullable.GetUnderlyingType(type);
                }
            }
            else
            {
                return type;
            }
        }
    }
    
    public class ReturnValue<T>
    {
        private T _retValue;
        private string _msg;
        public ReturnValue(T inA, string inMgs)
        {
            _retValue = inA;
            _msg = inMgs;
        }

        public T RetValue
        {
            get { return _retValue; }
            set { _retValue = value; }
        }
        public string Msg
        {
            get { return _msg; }
            set { _msg = value; }
        }

    }
}
